package com.inspur.s57.datacatalog.service.impl;

import com.inspur.s57.datacatalog.dao.CataLogDAO;
import com.inspur.s57.datacatalog.entity.CataLog;
import com.inspur.s57.datacatalog.service.CataLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Slf4j
@Service
public class CataLogServiceImpl implements CataLogService {

    @Autowired
    private CataLogDAO cataLogDAO;

    @Override
    public Page<CataLog> findCataLogByPage(String className,Pageable pageable) {
        if(StringUtils.isEmpty(className)){
            return cataLogDAO.findAll(pageable);
        }else {
            return cataLogDAO.findCataLogsByClassNameLike("%"+className+"%",pageable);
        }
    }

    @Override
    public CataLog findCataLogById(int id) {
        return cataLogDAO.findById(id).get();
    }

    @Override
    @Transactional(rollbackOn = Exception.class)
    public boolean saveCataLog(CataLog cataLog) {
        try{
            cataLogDAO.save(cataLog);
            return true;
        }catch (Exception e){
            log.error(e.getMessage());
            return false;
        }
    }

    @Override
    @Transactional(rollbackOn = Exception.class)
    public boolean deleteCataLogById(int id) {
        try{
            cataLogDAO.deleteById(id);
            return true;
        }catch (Exception e){
            log.error(e.getMessage());
            return false;
        }
    }

    @Override
    public List<CataLog> findAllCataLog() {
        return cataLogDAO.findAll();
    }

    @Override
    public List<CataLog> findCataLogsByType(String type) {
        return cataLogDAO.findCataLogsByType(type);
    }
}
